﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SQLite;
using DotSpatial.Projections;
using DotSpatial.Data;
using DotSpatial.Topology;
using DotSpatial.Controls;
using DotSpatial.Symbology;
using System.ComponentModel.Composition;


namespace SoilProfiler
{
    public partial class uxSoilProfiler : Form
    {

        private string _pathdb;
        private Databasemanagement database;
        private ProjectionInfo ProjectionData;
       
        private MapPointLayer _Wells;
        private MapPolygonLayer _Profile;
        MapLineLayer _ProfileLink;
        private FeatureSet _feature_Profile;
        private FeatureSet _feature_ProfileLink;
        private FeatureSet _feature_Wells;
        private Columns ProfileColumns;
        private List<Triangulator.Geometry.TriangleId> triangles;
        private ConfigurareTriangulation TrianglesLayers;
        private FeatureSet _feature_TIN;
        private DrawProfile _Draw;
        private MapPolygonLayer _PolygonLayer_TIN;

        [Export("Shell", typeof(ContainerControl))]
        private static ContainerControl Shell;

        /// <summary>
        /// Load  form
        /// </summary>
        public uxSoilProfiler()
        {
            Shell = this;
            InitializeComponent();
            appManager1.LoadExtensions();
            uxMainSpliter.SplitterDistance = uxMainSpliter.Height;

            uxMap.Projection = KnownCoordinateSystems.Projected.World.WebMercator;
            uxMapProfile.Projection = KnownCoordinateSystems.Projected.World.WebMercator;
            EnableMenu(false);
             uxMap.MouseClick +=new MouseEventHandler(uxMap_MouseClick);
            uxMap.MouseUp += new MouseEventHandler(uxMap_MouseUp);
        }


        /// <summary>
        /// It is executed when mouse click over map
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxMap_MouseClick(object sender, EventArgs e)
        {
            if (_feature_Wells != null)
            {
               if(idFunction==1)
                  _Wells.Selection.Clear();
              

            }

        }

        /// <summary>
        /// It is used to select object in the map
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxMap_MouseUp(object sender, EventArgs e)
        {
            //option 3 => Draw profiles of a triangle selected
            if (idFunction == 3)
            {
                if (_PolygonLayer_TIN == null)
                {

                    uxMap.FunctionMode = FunctionMode.None;
                
                }
                var sel = _PolygonLayer_TIN.Selection;
                List<IFeature> ifs = sel.ToFeatureList();
                if (ifs.Count >= 1)
                {
                    uxProfileList.Items.Clear();
                    IFeature fea = ifs[0];
                    int id = Convert.ToInt32(fea.DataRow["id"]);
                    //Draw profiles of profile
                    DrawLinksProfiles(id);
                    
                }
                _PolygonLayer_TIN.Selection.Clear();
                
                return;
            }

            //option 1=> Draw profiles selected without links
            if (_feature_Wells != null)
            {
                try
                {
                    uxProfileList.Items.Clear();


                    var sel = _Wells.Selection;
                    List<IFeature> ifs = sel.ToFeatureList();
                    CreateProfileLayer();
                    double range = ProfileColumns.MaxValue() - ProfileColumns.MinValue();

                    int i = 0;
                    foreach (IFeature item in ifs)
                    {
                        DrawListProfiles(i, item, range / 15);
                        i++;
                    }
                    CreateProfileColumns();
                    uxData.DataSource = _feature_Profile.DataTable;

                }
                catch
                {

                    return;
                }

            }


        }


        /// <summary>
        /// Draw profiles of a Triangle  => the first profile is repeated
        /// </summary>
        /// <param name="id">Id profile</param>
        private void DrawLinksProfiles(int id)
        {
            Dictionary<int, int> interIndex = new Dictionary<int, int>();
            foreach (Triangulator.Geometry.TriangleId item in triangles)
            {
                if (item.id == id)
                {

                    CreateProfileLayer();
                    double range = ProfileColumns.MaxValue() - ProfileColumns.MinValue();

                    ProfileCreation(0, range / 15, _IndexVertices[item.triangle.p1]);

                    ProfileCreation(4, range / 15, _IndexVertices[item.triangle.p2]);

                    ProfileCreation(8, range / 15, _IndexVertices[item.triangle.p3]);

                    ProfileCreation(12, range / 15, _IndexVertices[item.triangle.p1]);
                    interIndex.Add(_IndexVertices[item.triangle.p1], 0);
                    interIndex.Add(_IndexVertices[item.triangle.p2], 4);
                    interIndex.Add(_IndexVertices[item.triangle.p3], 8);


                    double valdist = range / 15;

                    foreach (Volume item3 in TrianglesLayers.VolumeInfo)
                    {
                        if (item3.id == id)
                        {
                            foreach (LayerTriangle3D item4 in item3.Layers)
                            {

                                
                                Coordinate[] cor = new Coordinate[2];
                                cor[0] = new Coordinate(10000 + valdist * (interIndex[item4.vertex[0].Point2D.id] + 1), 10000 + item4.vertex[0].Z);
                                cor[1] = new Coordinate(10000 + valdist * interIndex[item4.vertex[1].Point2D.id], 10000 + item4.vertex[1].Z);
                                LinearRing l = new LinearRing(cor);

                                IFeature ne = _feature_ProfileLink.AddFeature(l);

                                Coordinate[] cor1 = new Coordinate[2];
                                cor1[0] = new Coordinate(10000 + valdist * (interIndex[item4.vertex[1].Point2D.id] + 1), 10000 + item4.vertex[1].Z);
                                cor1[1] = new Coordinate(10000 + valdist * interIndex[item4.vertex[2].Point2D.id], 10000 + item4.vertex[2].Z);
                                LinearRing l1 = new LinearRing(cor1);
                                IFeature ne1 = _feature_ProfileLink.AddFeature(l1);

                                Coordinate[] cor2 = new Coordinate[2];
                                cor2[0] = new Coordinate(10000 + valdist * (interIndex[item4.vertex[2].Point2D.id] + 1), 10000 + item4.vertex[2].Z);
                                cor2[1] = new Coordinate(10000 + (valdist * 12), 10000 + item4.vertex[0].Z);
                                LinearRing l2 = new LinearRing(cor2);
                                IFeature ne2 = _feature_ProfileLink.AddFeature(l2);





                            }
                        }
                    }

                    uxData.DataSource = _feature_Profile.DataTable;

                    CreateProfileColumns();

                }
            }
        }

        /// <summary>
        /// This function creates the layers in which profiles and links to be populated
        /// </summary>
        private void CreateProfileColumns()
        {
            Dictionary<string, Color> color = DrawProfile.ListColors(database);
            _ProfileLink = new MapLineLayer(_feature_ProfileLink);
            
            _Profile = new MapPolygonLayer(_feature_Profile);
            IPolygonSymbolizer sim = new PolygonSymbolizer(Color.White);
            _Profile.Symbolizer = sim;
            PolygonScheme pol = new PolygonScheme();
            pol.EditorSettings.FieldName = "unit";
            pol.EditorSettings.ClassificationType = ClassificationType.Custom;

            for (int j = 0; j < color.Keys.Count; j++)
            {
                PolygonCategory cat = new PolygonCategory();
                cat.SetColor(color[color.Keys.ToArray()[j]]);
                cat.FilterExpression = string.Format("[unit]='{0}'", color.Keys.ToArray()[j]);
                cat.LegendText = color.Keys.ToArray()[j];
                cat.Symbolizer.SetOutline(Color.Gray, 0.1);
                pol.AddCategory(cat);
            }
            PolygonCategory catd = new PolygonCategory();
            catd.SetColor(Color.White);
            catd.Symbolizer.SetOutline(Color.White, 0.1);
            catd.FilterExpression = "[unit]=''";
            pol.AddCategory(catd);


            _Profile.Symbology = pol;
            _Profile.LegendText = "Profiles";
            _ProfileLink.LegendText = "Links";
            uxMapProfile.AddLabels(_Profile, "[wellid]", new Font("Tahoma", (float)6.0), Color.Black);
            uxMapProfile.Layers.Add(_ProfileLink);
            uxMapProfile.Layers.Add(_Profile);
            uxMapProfile.ViewExtents = _Profile.Extent;

        }




        /// <summary>
        /// Create the profile according with position 
        /// </summary>
        /// <param name="id">id position </param>
        /// <param name="fea">Profile featue</param>
        /// <param name="scalex">width profile</param>
        public void DrawListProfiles(int id, IFeature fea, double scalex)
        {
            if (fea.DataRow["hydroid"] == DBNull.Value) return;
            int idv = Convert.ToInt32(fea.DataRow["hydroid"]);
            ProfileCreation(id, scalex, idv);
            //Add profile to list
           uxProfileList.Items.Add(idv);
        }

        /// <summary>
        /// Create the profile according with position 
        /// </summary>
        /// <param name="id">id position</param>
        /// <param name="scalex">width profile</param>
        /// <param name="idv">Id profile</param>
        private void ProfileCreation(int id, double scalex, int idv)
        {
            IEnumerable<LayerColumn> values = ProfileColumns.GetProfile(idv);
            int ids = values.Count();
            int i = 0;
            foreach (LayerColumn item2 in values)
            {
                LayerColumn item =(LayerColumn) item2.Clone();//
                IFeature newF = _feature_Profile.AddFeature( item.GetPolygon(10000 + scalex * id, 10000, scalex));
                newF.DataRow["id"] = item.Id;
                if (i == 0)
                    newF.DataRow["wellid"] = item.Profile;

                newF.DataRow["refelev"] = item.refelev;
                newF.DataRow["fromdepth"] = item.fromdepth;
                newF.DataRow["todepth"] = item.todepth;
                newF.DataRow["topelev"] = item.topelev;
                newF.DataRow["unit"] = item.Unit;
                newF.DataRow["description"] = item.description;
                //  uxProfileList.Items.Add(item.Profile);
                i++;
            }
            uxProfileList.Items.Add(idv);
        }






        /// <summary>
        /// Configure layers of profiles
        /// </summary>
        private void CreateProfileLayer()
        {

            RemoveLayer(uxMapProfile, "Profiles");
            RemoveLayer(uxMapProfile, "Links");
            FeatureSet featurel = new FeatureSet(FeatureType.Line);
            FeatureSet feature = new FeatureSet(FeatureType.Polygon);
            feature.Projection = uxMapProfile.Projection;
            featurel.Projection = uxMapProfile.Projection;
            feature.DataTable.Columns.Add(new DataColumn("id", typeof(int)));
            featurel.DataTable.Columns.Add(new DataColumn("id", typeof(int)));
            feature.DataTable.Columns.Add(new DataColumn("wellid", typeof(int)));
            feature.DataTable.Columns.Add(new DataColumn("refelev", typeof(double)));
            feature.DataTable.Columns.Add(new DataColumn("fromdepth", typeof(double)));
            feature.DataTable.Columns.Add(new DataColumn("todepth", typeof(double)));
            feature.DataTable.Columns.Add(new DataColumn("topelev", typeof(double)));
            feature.DataTable.Columns.Add(new DataColumn("bottomelev", typeof(double)));
            feature.DataTable.Columns.Add(new DataColumn("unit", typeof(string)));
            feature.DataTable.Columns.Add(new DataColumn("description", typeof(string)));
            _feature_Profile = feature;
            _feature_ProfileLink = featurel;
        }


        /// <summary>
        /// Control options
        /// </summary>
        /// <param name="eval"></param>
        private void EnableMenu(bool eval)
        {
            viewToolStripMenuItem.Enabled = eval;
            dataManagementToolStripMenuItem.Enabled = eval;
            toolsToolStripMenuItem.Enabled = eval;
            uxViewMap.Enabled = eval;
            uxViewProfile.Enabled = eval;
        }
        
        private void uxButton_Click(object sender, EventArgs e)
        {
            uxMap.AddRasterLayer();

        }

        private void dataManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Create a new project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uxNewProject np = new  uxNewProject();
            if (np.ShowDialog() == DialogResult.OK)
            { 
            
               _pathdb=np.uxFolder.Text +"\\"+ np.uxName.Text+".xprofile";
                if (!File.Exists(_pathdb))
                {
                    SQLiteConnection.CreateFile(_pathdb);
                    database = new Databasemanagement(_pathdb);
                    database.CreateTables();
                    database.ConfigurationProject(np.uxName.Text, np.uxFolder.Text, np.uxDescription.Text, np.Projection);
                    if (np.Projection.Contains("PROJCS"))
                        ProjectionData = ProjectionInfo.FromEsriString(np.Projection);
                    else
                        ProjectionData = ProjectionInfo.FromProj4String(np.Projection);
                    EnableMenu(true);
                }
                else
                {
                    MessageBox.Show("The file already exists");
                }
            }
        }

        /// <summary>
        /// Open a project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uxOpenProject op = new uxOpenProject();
            if (op.ShowDialog() == DialogResult.OK) {
                _pathdb = op.uxFileName.Text;
                database = new Databasemanagement(_pathdb);
                string value=database.ExecuteScalar("select projection from project limit 1");
                if (value.Contains("PROJCS"))
                    ProjectionData = ProjectionInfo.FromEsriString(value);
                else
                    ProjectionData = ProjectionInfo.FromProj4String(value);

                EnableMenu(true);
            }
        }

        /// <summary>
        /// Import Wells
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wellsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uxImportWells well = new uxImportWells();
            if (well.ShowDialog() == DialogResult.OK)
            {
                  database.Importwells(well.uxFileName.Text, well.Projection);
                  
            }

        }

        /// <summary>
        /// Draw Well
        /// </summary>
        private void DrawWells()
        {
            //Read Information in the database
            DataTable datap = database.GetDataTable("Select * from wells");
            
            
            if (datap.Rows.Count>0)
               RemoveLayer(uxMap, "Wells");



              FeatureSet feature = new FeatureSet(FeatureType.Point);

               feature.Projection = uxMap.Projection;
               feature.DataTable.Columns.Add(new DataColumn("objectid", typeof(int)));
               feature.DataTable.Columns.Add(new DataColumn("hydroid", typeof(int)));
               feature.DataTable.Columns.Add(new DataColumn("ftype", typeof(string)));
               feature.DataTable.Columns.Add(new DataColumn("x", typeof(double)));
               feature.DataTable.Columns.Add(new DataColumn("y", typeof(double)));
               IPoint point;
               Dictionary<int, Coordinate> listCoor = new Dictionary<int, Coordinate>();
               
             //Populate featureset from database
               foreach (DataRow item in datap.Rows)
               {
                   point = new DotSpatial.Topology.Point();
                   double[] xy = new double[2];
                   xy[0] = Convert.ToDouble(item["x"]);
                   xy[1] = Convert.ToDouble(item["y"]);
                   Reproject.ReprojectPoints(xy, new double[] { 0 }, ProjectionData, uxMap.Projection, 0, 1);
                   point.X = xy[0];
                   point.Y = xy[1];

                   IFeature fea = feature.AddFeature(point);
                   fea.DataRow["objectid"] = Convert.ToInt32(item["objectid"]);
                   fea.DataRow["hydroid"] = Convert.ToInt32(item["hydroid"]);
                   fea.DataRow["ftype"] = Convert.ToString(item["ftype"]);
                   fea.DataRow["x"] = Convert.ToDouble(item["x"]);
                   fea.DataRow["y"] = Convert.ToDouble(item["y"]);
                   listCoor.Add(Convert.ToInt32(item["hydroid"]), new Coordinate(Convert.ToDouble(item["x"]),Convert.ToDouble(item["y"])));
                   feature.AddFeature(fea);

               }
               feature.Name = "Wells";
               _feature_Wells = feature;
               _Wells = new MapPointLayer(feature);
               _Wells.LegendText = "Wells";
               uxData.DataSource = feature.DataTable;

               uxMap.Layers.Add(_Wells);


               //Get profile information from database
               DataTable data = database.GetDataTable("Select b.id as id, b.wellid as wellid, b.refelev as refelev,b.todepth as todepth,b.fromdepth as fromdepth, b.topelev as topelev, b.bottomelev as bottomelev,h.hguname as unit, h.description as description from borehole b, hgus h where b.hguid= h.hydroid order by b.id ");

               //Create an instance of all profiles (including layers)
               ProfileColumns = new Columns(data, listCoor, ProjectionData);


        }

        /// <summary>
        /// Remove a lare
        /// </summary>
        /// <param name="_mapArgs">Map</param>
        /// <param name="name">Layer name</param>
        public static void RemoveLayer(Map _mapArgs, string name)
        {

            //_mapArgs.FunctionMode = FunctionMode.None;
            foreach (IMapLayer lay in _mapArgs.GetLayers())
            {
                if (lay.LegendText == name)
                {
                    _mapArgs.Layers.Remove(lay);

                }
            }

        }

        /// <summary>
        /// Check if a layer exist
        /// </summary>
        /// <param name="_mapArgs">Map</param>
        /// <param name="name">Layer name</param>
        /// <returns>Layer</returns>
        public static IMapLayer AvailableLayer(Map _mapArgs, string name)
        {

            //_mapArgs.FunctionMode = FunctionMode.None;
            foreach (IMapLayer lay in _mapArgs.GetLayers())
            {
                if (lay.LegendText == name)
                {
                    return lay;

                }
            }
            return null;

        }

        /// <summary>
        /// Check if a layer exist
        /// </summary>
        /// <param name="_mapArgs">Map</param>
        /// <param name="name">Layer name</param>
        /// <returns>True or false is the layer exists</returns>
        public bool LayerExist(Map mainMap,string name)
        {
            foreach (IMapLayer lay in mainMap.GetAllLayers())
            {
                if (lay.LegendText == name)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Import Geologic Units
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void soilUnitsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uxImportSoilUnits u = new uxImportSoilUnits();
            if (u.ShowDialog() == DialogResult.OK)
            {
                database.Importhgus(u.uxFileName.Text);

            }
        }

        /// <summary>
        /// Import information of each well
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void boreholesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uxImportBoreHoles u = new uxImportBoreHoles();
            if (u.ShowDialog() == DialogResult.OK)
            {
                database.Importborehole(u.uxFileName.Text);

            }
        }

        int idFunction = 0;

        /// <summary>
        /// This function allows user selects wells
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxViewProfile_Click(object sender, EventArgs e)
        {
            idFunction = 1;
            uxMap.FunctionMode = FunctionMode.Select;
           // uxMap.MouseUp += uxMap_MouseUp;
        }

        private void viewToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Draw wells
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxViewMap_Click(object sender, EventArgs e)
        {
            DrawWells();
            
        }

        /// <summary>
        /// This function calculate triangualtion and configure the program
        /// to interpolate a transect
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxTriangulation_Click(object sender, EventArgs e)
        {
            if (_feature_Wells != null)
            {
                triangles = Calculate();
                //Create an instance to draw a transect
                _Draw = new DrawProfile(uxMap, database,TrianglesLayers, _feature_TIN, uxZedGraphControl1, uxTools);
              
            }
        }



        Dictionary<int, int> _IndexVertices;

        /// <summary>
        /// Calculate triangualtion
        /// </summary>
        /// <returns></returns>
        private  List<Triangulator.Geometry.TriangleId> Calculate()
        {
            //DelaunyTriangulation generate the TIN
            DelaunyTriangulation delauny = new DelaunyTriangulation();
            List<Triangulator.Geometry.Point> Vertex = new List<Triangulator.Geometry.Point>();
            _IndexVertices = new Dictionary<int, int>();
            int idv = 0;
            foreach (IFeature item in _feature_Wells.Features)
            {
                if (item.DataRow["hydroid"] != DBNull.Value)
                {
                    var points = item.BasicGeometry.Coordinates[0];
                    Triangulator.Geometry.Point p = new Triangulator.Geometry.Point(points.X, points.Y);
                    p.id=Convert.ToInt32(item.DataRow["hydroid"]);
                    Vertex.Add(p);
                    _IndexVertices.Add(idv, p.id);
                    idv++;
                }
            }

            //Calculate triangulation
            List<Triangulator.Geometry.Triangle> triangles = delauny.Triangulate(Vertex);

            
            List<Triangulator.Geometry.TriangleId> trianglesid = new List<Triangulator.Geometry.TriangleId>();
            
            //create featureset of Triangulation
            FeatureSet feature = new FeatureSet(FeatureType.Polygon);
            feature.Projection = uxMapProfile.Projection;
            feature.DataTable.Columns.Add(new DataColumn("id", typeof(int)));

            int id=0;
            foreach (Triangulator.Geometry.Triangle tria in triangles)
            {

                Coordinate[] array = new Coordinate[4];
                array[0] = new Coordinate(Vertex[tria.p1].X, Vertex[tria.p1].Y);
                array[1] = new Coordinate(Vertex[tria.p2].X, Vertex[tria.p2].Y);
                array[2] = new Coordinate(Vertex[tria.p3].X, Vertex[tria.p3].Y);
                array[3] = new Coordinate(Vertex[tria.p1].X, Vertex[tria.p1].Y);
                LinearRing shell = new LinearRing(array);
                Polygon poly = new Polygon(shell);
                IFeature newp=feature.AddFeature(poly);
                newp.DataRow["id"] = id;

                trianglesid.Add(new Triangulator.Geometry.TriangleId(tria, id));
                id++;
            }
            _feature_TIN = feature;
            _PolygonLayer_TIN = new MapPolygonLayer(feature);
            _PolygonLayer_TIN.LegendText = "Grid";
            uxMap.Layers.Add(_PolygonLayer_TIN);

            //Configure the triangulation of all layers in each triangle
            TrianglesLayers = new ConfigurareTriangulation(ProfileColumns, trianglesid, Vertex);
            TrianglesLayers.Calculate();
            return trianglesid;
        }

        private void uxSelectTriangle_Click(object sender, EventArgs e)
        {
          
        }

       /// <summary>
        ///  //Start Draw transect
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void uxDraw_Click(object sender, EventArgs e)
        {
            // We're expecting this extension to only be run in a Windows Forms application.
            // We'll depend on a few Windows Forms (Map) features like MouseDown, so we cast
            // the App.Map as a Map and store a reference to it.
            idFunction = 2;
            if (_Draw != null)
            {
                _Draw.StartDrawProfile();
                int height = uxMainSpliter.Height;

                uxMainSpliter.SplitterDistance = (int)(height * 0.7);

            }


            // Let the user know we are ready for them to set points by changing the cursor.

        }
        /// <summary>
        /// Call 3d view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dViewComingSoonToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Dictionary<string, Color> color = DrawProfile.ListColors(database);

            frm3Dview f3d = new frm3Dview(TrianglesLayers, ProfileColumns, color, _Draw);



            f3d.Show();
        }

        /// <summary>
        /// Select Triangle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxSelectTriangle_Click_1(object sender, EventArgs e)
        {
           
            if (triangles != null)
            {
                uxMap.FunctionMode = FunctionMode.Select;

                idFunction = 3;

            }
        }

        private void defineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_feature_Wells != null)
            {
                triangles = Calculate();
                //Create an instance to draw a transect
                _Draw = new DrawProfile(uxMap, database, TrianglesLayers, _feature_TIN, uxZedGraphControl1, uxTools);

            }
        }


    }
}
